package com.profound.notes.mvvm;

import java.util.HashMap;

/**
 * @author zjwujlei
 * @date 2021/4/24
 * @email zjwujlei@sina.com
 * @description
 */
public class ViewModelStore {

    private final HashMap<String,ViewModel> mMap = new HashMap();

    final void put(String key,ViewModel viewModel){
        ViewModel old = mMap.get(key);
        if(old != null){
            old.onCleared();
        }

        mMap.put(key,viewModel);
    }

    final ViewModel get(String key){
        return mMap.get(key);
    }

    public final void clear(){
        for(ViewModel vm : mMap.values()){
            vm.onCleared();
        }
        mMap.clear();
    }

}
